package edu.paper.job.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import edu.paper.job.dao.JobDao;
import edu.paper.job.domain.Employee;
import edu.paper.job.domain.Job;
import edu.paper.job.service.EmployeeService;
import edu.paper.job.service.JobService;

/**
 * 工作业务逻辑
 * @author bowen
 *
 */
@Service
public class JobServiceImpl implements JobService{

	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private JobDao jobDao;
	
	@Autowired
	private EmployeeService employeeService;
	
	@Override
	public List<Job> find(Job job) {
		return jobDao.find(job);
	}

	@Override
	public Job get(Serializable id) {
		return jobDao.get(id);
	}

	@Override
	public void insert(Job job) {
		jobDao.insert(job);
	}

	@Override
	public void update(Job job) {
		jobDao.update(job);
	}

	@Override
	public void deleteById(Serializable id) {
		jobDao.deleteById(id);
	}

	@Override
	public void delete(Serializable[] ids) {
		jobDao.delete(ids);
	}

	@Override
	public void deleteJob(Job job) {
		//假删除
		job.setIsDeleted(true);
		jobDao.update(job);
		//检查是否可以真的删除招聘信息
		/*
		 * 真删除招聘信息需要满足一些条件，企业删除了招聘信息，求职者不再浏览应聘信息。
		 */
		//检查招聘信息是否可以删除
		job = jobDao.get(job.getId());
		if(job.getIsDeleted()) {//企业删除了该招聘信息
			//检查应聘者是否不再浏览招聘信息，即包含该招聘的雇佣关系都被应聘者删除了
			Employee params = applicationContext.getBean(Employee.class);
			params.getJob().setId(job.getId());
			params.setIsApplicantDeleted(false);
			List<Employee> employees = employeeService.find(params);
			if(employees.size() == 0) {
				//没有应聘者要浏览该招聘信息
				jobDao.deleteById(job.getId());
			}
		}
		
	}

	@Override
	public void insertJob(Job job) {
		//新建一个工作，肯定不是删除的
		job.setIsDeleted(false);
		jobDao.insert(job);
	}

	@Override
	public void publishJob(Job job) {
		job.setIsPublic(true);
		jobDao.update(job);
		
	}

	@Override
	public void updateJob(Job job) {
		jobDao.update(job);
	}

	@Override
	public void coverJob(Job job) {
		job.setIsPublic(false);
		jobDao.update(job);
	}

	@Override
	public List<Job> listCompanyJobs(Job job) {
		//列出企业没删除的工作
		job.setIsDeleted(false);
		return jobDao.find(job);
		
	}

	@Override
	public List<Job> listPublishedJobs(Job job) {
		//列出企业没被删除，但发布的企业
		job.setIsDeleted(false);
		job.setIsPublic(true);
		return jobDao.find(job);
	}

	@Override
	public List<Job> query(Job job) {
		//模糊查询招聘信息
		return jobDao.query(job);
	}
	
	@Override
	public List<Job> queryJob(Job job) {
		//企业未删除发布的招聘
		job.setIsDeleted(false);
		job.setIsPublic(true);
		return jobDao.query(job);
	}


}
